{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import iplantuml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 数学模型(猜车牌游戏)\n",
    "\n",
    "$a_1, a_2, a_3, a_4$ 分别四位数字车牌号的数字\n",
    "\n",
    "$\n",
    "\\begin{cases}\n",
    "    a_1 = a_2 & 1 \\leq a_1 \\leq 9, 0 \\leq a_2 \\leq 9 \\\\\n",
    "    a_3 = a_4 & a_3 \\geq 0, a_4 \\leq 9 \\\\\n",
    "    a_1 \\neq a_3 \\\\\n",
    "    1000 \\times a_1  + 100 \\times a_2 + 10 \\times a_3 + a_4 = x^2 & x \\in {\\Bbb Z}\n",
    "\\end{cases}\n",
    "$\n",
    "\n",
    "## 2. 算法设计(枚举)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing output for /home/jack/Desktop/structures-algorithms/9b9cb8fc-ee77-4350-9a82-da0482f3c5f5.uml to 9b9cb8fc-ee77-4350-9a82-da0482f3c5f5.svg\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" contentStyleType=\"text/css\" height=\"485px\" preserveAspectRatio=\"none\" style=\"width:370px;height:485px;background:#FFFFFF;\" version=\"1.1\" viewBox=\"0 0 370 485\" width=\"370px\" zoomAndPan=\"magnify\"><defs/><g><ellipse cx=\"183.6704\" cy=\"20\" fill=\"#222222\" rx=\"10\" ry=\"10\" style=\"stroke:#222222;stroke-width:1.0;\"/><rect fill=\"#F1F1F1\" height=\"61.9063\" rx=\"12.5\" ry=\"12.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"142.7782\" x=\"112.2813\" y=\"50\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"12\" lengthAdjust=\"spacing\" textLength=\"122.7782\" x=\"122.2813\" y=\"71.1387\">定义变量 i 表示前两位,</text><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"12\" lengthAdjust=\"spacing\" textLength=\"94.9635\" x=\"122.2813\" y=\"85.1074\">变量 j 表示后两位</text><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"12\" lengthAdjust=\"spacing\" textLength=\"64.9336\" x=\"122.2813\" y=\"99.0762\">i = 1, j = 0</text><rect fill=\"#F1F1F1\" height=\"33.9688\" rx=\"12.5\" ry=\"12.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"80.0005\" x=\"143.6702\" y=\"307.9063\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"12\" lengthAdjust=\"spacing\" textLength=\"60.0005\" x=\"153.6702\" y=\"329.0449\">输出车牌号</text><polygon fill=\"#F1F1F1\" points=\"75,263.9063,292.3408,263.9063,304.3408,275.9063,292.3408,287.9063,75,287.9063,63,275.9063,75,263.9063\" style=\"stroke:#181818;stroke-width:0.5;\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"11\" lengthAdjust=\"spacing\" textLength=\"217.3408\" x=\"75\" y=\"279.7144\">1000*i + 100*i + 10*j +j 是某整数的平方</text><polygon fill=\"#F1F1F1\" points=\"183.6704,361.875,195.6704,373.875,183.6704,385.875,171.6704,373.875,183.6704,361.875\" style=\"stroke:#181818;stroke-width:0.5;\"/><polygon fill=\"#F1F1F1\" points=\"166.8078,219.9063,200.5329,219.9063,212.5329,231.9063,200.5329,243.9063,166.8078,243.9063,154.8078,231.9063,166.8078,219.9063\" style=\"stroke:#181818;stroke-width:0.5;\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"11\" lengthAdjust=\"spacing\" textLength=\"26.7319\" x=\"170.3044\" y=\"235.7144\">i != j</text><polygon fill=\"#F1F1F1\" points=\"183.6704,405.875,195.6704,417.875,183.6704,429.875,171.6704,417.875,183.6704,405.875\" style=\"stroke:#181818;stroke-width:0.5;\"/><polygon fill=\"#F1F1F1\" points=\"164.1814,175.9063,203.1594,175.9063,215.1594,187.9063,203.1594,199.9063,164.1814,199.9063,152.1814,187.9063,164.1814,175.9063\" style=\"stroke:#181818;stroke-width:0.5;\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"11\" lengthAdjust=\"spacing\" textLength=\"35.4814\" x=\"167.678\" y=\"191.7144\">j &lt;= 9</text><polygon fill=\"#F1F1F1\" points=\"165.9297,131.9063,201.4111,131.9063,213.4111,143.9063,201.4111,155.9063,165.9297,155.9063,153.9297,143.9063,165.9297,131.9063\" style=\"stroke:#181818;stroke-width:0.5;\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"11\" lengthAdjust=\"spacing\" textLength=\"35.4814\" x=\"165.9297\" y=\"147.7144\">i &lt;= 9</text><ellipse cx=\"24\" cy=\"186.9063\" fill=\"none\" rx=\"11\" ry=\"11\" style=\"stroke:#222222;stroke-width:1.0;\"/><ellipse cx=\"24\" cy=\"186.9063\" fill=\"#222222\" rx=\"6\" ry=\"6\" style=\"stroke:#222222;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"183.6704\" x2=\"183.6704\" y1=\"30\" y2=\"50\"/><polygon fill=\"#181818\" points=\"179.6704,40,183.6704,50,187.6704,40,183.6704,44\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"183.6704\" x2=\"183.6704\" y1=\"287.9063\" y2=\"307.9063\"/><polygon fill=\"#181818\" points=\"179.6704,297.9063,183.6704,307.9063,187.6704,297.9063,183.6704,301.9063\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"304.3408\" x2=\"316.3408\" y1=\"275.9063\" y2=\"275.9063\"/><polygon fill=\"#181818\" points=\"312.3408,314.8906,316.3408,324.8906,320.3408,314.8906,316.3408,318.8906\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"316.3408\" x2=\"316.3408\" y1=\"275.9063\" y2=\"373.875\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"316.3408\" x2=\"195.6704\" y1=\"373.875\" y2=\"373.875\"/><polygon fill=\"#181818\" points=\"205.6704,369.875,195.6704,373.875,205.6704,377.875,201.6704,373.875\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"183.6704\" x2=\"183.6704\" y1=\"341.875\" y2=\"361.875\"/><polygon fill=\"#181818\" points=\"179.6704,351.875,183.6704,361.875,187.6704,351.875,183.6704,355.875\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"183.6704\" x2=\"183.6704\" y1=\"243.9063\" y2=\"263.9063\"/><polygon fill=\"#181818\" points=\"179.6704,253.9063,183.6704,263.9063,187.6704,253.9063,183.6704,257.9063\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"212.5329\" x2=\"326.3408\" y1=\"231.9063\" y2=\"231.9063\"/><polygon fill=\"#181818\" points=\"322.3408,314.8906,326.3408,324.8906,330.3408,314.8906,326.3408,318.8906\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"326.3408\" x2=\"326.3408\" y1=\"231.9063\" y2=\"417.875\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"326.3408\" x2=\"195.6704\" y1=\"417.875\" y2=\"417.875\"/><polygon fill=\"#181818\" points=\"205.6704,413.875,195.6704,417.875,205.6704,421.875,201.6704,417.875\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"183.6704\" x2=\"183.6704\" y1=\"385.875\" y2=\"405.875\"/><polygon fill=\"#181818\" points=\"179.6704,395.875,183.6704,405.875,187.6704,395.875,183.6704,399.875\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"183.6704\" x2=\"183.6704\" y1=\"199.9063\" y2=\"219.9063\"/><polygon fill=\"#181818\" points=\"179.6704,209.9063,183.6704,219.9063,187.6704,209.9063,183.6704,213.9063\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"183.6704\" x2=\"183.6704\" y1=\"429.875\" y2=\"439.875\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"183.6704\" x2=\"344.3408\" y1=\"439.875\" y2=\"439.875\"/><polygon fill=\"#181818\" points=\"340.3408,322.8906,344.3408,312.8906,348.3408,322.8906,344.3408,318.8906\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"344.3408\" x2=\"344.3408\" y1=\"187.9063\" y2=\"439.875\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"344.3408\" x2=\"215.1594\" y1=\"187.9063\" y2=\"187.9063\"/><polygon fill=\"#181818\" points=\"225.1594,183.9063,215.1594,187.9063,225.1594,191.9063,221.1594,187.9063\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"152.1814\" x2=\"49\" y1=\"187.9063\" y2=\"187.9063\"/><polygon fill=\"#181818\" points=\"45,308.8906,49,318.8906,53,308.8906,49,312.8906\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"49\" x2=\"49\" y1=\"187.9063\" y2=\"451.875\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"49\" x2=\"356.3408\" y1=\"451.875\" y2=\"451.875\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"356.3408\" x2=\"356.3408\" y1=\"143.9063\" y2=\"451.875\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"356.3408\" x2=\"213.4111\" y1=\"143.9063\" y2=\"143.9063\"/><polygon fill=\"#181818\" points=\"223.4111,139.9063,213.4111,143.9063,223.4111,147.9063,219.4111,143.9063\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"183.6704\" x2=\"183.6704\" y1=\"155.9063\" y2=\"175.9063\"/><polygon fill=\"#181818\" points=\"179.6704,165.9063,183.6704,175.9063,187.6704,165.9063,183.6704,169.9063\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"153.9297\" x2=\"24\" y1=\"143.9063\" y2=\"143.9063\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"24\" x2=\"24\" y1=\"143.9063\" y2=\"175.9063\"/><polygon fill=\"#181818\" points=\"20,165.9063,24,175.9063,28,165.9063,24,169.9063\" style=\"stroke:#181818;stroke-width:1.0;\"/><line style=\"stroke:#181818;stroke-width:1.0;\" x1=\"183.6704\" x2=\"183.6704\" y1=\"111.9063\" y2=\"131.9063\"/><polygon fill=\"#181818\" points=\"179.6704,121.9063,183.6704,131.9063,187.6704,121.9063,183.6704,125.9063\" style=\"stroke:#181818;stroke-width:1.0;\"/><!--SRC=[Aov9B2fHuB9wkcxMavsTJ_jdl6plLyXKUB5mnVCbkvvszZxPiUJ9tbuT1IweL1PCQa8VH8ehKy5MmL07A66hO63DLPwHcPEgaQbWOwjWgScb00GG4Q0yGWW4CjCKD80MATe2PH2YC1b30mC3hKm5RGL3EAsLfQ2TfV1inlfdyoSycxhbsTGDpsUrFDsv-TcqdQZwGS3gnRx9JzjtlTYxx7bdpzF-xTOeAbBpKZBJk31vG1ROjLnmHd59VW40]--></g></svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "%%plantuml\n",
    "@startuml\n",
    "start \n",
    ":定义变量 i 表示前两位, \n",
    "变量 j 表示后两位\n",
    "i = 1, j = 0;\n",
    "while(i <= 9)\n",
    "    while( j <= 9)\n",
    "        if ( i != j )\n",
    "            if (1000*i + 100*i + 10*j +j 是某整数的平方)\n",
    "                :输出车牌号;\n",
    "            endif\n",
    "        endif\n",
    "    endwhile\n",
    "endwhile\n",
    "stop\n",
    "@enduml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 代码实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7744\n"
     ]
    }
   ],
   "source": [
    "def test(k):\n",
    "    for i in range(32, 100):\n",
    "        if i ** 2 == k:\n",
    "            return k\n",
    "\n",
    "def guest():\n",
    "    for i in range(1, 10):\n",
    "        for j in range(10):\n",
    "            k = 1000*i + 100*i + 10*j + j\n",
    "            if test(k):\n",
    "                print(k)\n",
    "\n",
    "guest()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
